 module game.world;

import std.container;
import std.typetuple;
import std.array;

import gamelib.types;
import gamelib.util;
import gamelib.graphics.renderer2d.renderer;
import gamelib.graphics.renderer2d.textureview;
import gamelib.graphics.renderer2d.font;

import game.game;
import game.controls;
import game.units;
import game.battle.battlescene;

final class World
{
private:
    TextureManager mTextureMan;
    FontManager    mFontMan;

    bool mQuitReq = false;

    auto mStrBuffer = appender!(char[])();

    BattleScene mBattle;
public:
    enum UpdateRate = 50;
    enum UpdateInterval = 1000 / UpdateRate;
    enum Width = 800;
    enum Height = 600;

    @property auto textureManager() inout pure nothrow { return mTextureMan; }
    @property auto fontManager()    inout pure nothrow { return mFontMan; }

    this(TextureManager tman, FontManager fman)
    {
        mTextureMan = tman;
        mFontMan = fman;
        mBattle = new BattleScene(this);
    }

    void handleQuit() pure nothrow
    {
        mQuitReq = true;
    }

    void processAction(in Actions ac, bool pressed) pure
    {
        mBattle.processAction(ac, pressed);
    }

    bool update()
    {
        if(mQuitReq) 
        {
            return false;
        }

        return mBattle.update();
    }

    void draw(Renderer ren)
    {
        mBattle.draw(ren);
    }

    auto formatStr(Char, Args...)(in Char[] fmt, Args args)
    {
        import std.format;
        mStrBuffer.clear();
        formattedWrite(mStrBuffer, fmt, args);
        return mStrBuffer.data;
    }
}

